import { reportError } from './index';
// Check the network
export const checkNetWork = (callback,errFunc) => {
  uni.getNetworkType({
    success: res => {
      if (res.networkType !== "none") {
        callback();
      } else {
        console.log("Network is none");
        errFunc && errFunc({
          ErrorCode: 101,
          ErrorMsg: "网络异常，请稍后重试"
        });
      }
    },
    fail: err => {
      console.log("Get Network exception",err);
      errFunc && errFunc({
        ErrorCode: 101,
        ErrorMsg: "网络异常，请稍后重试"
      });
    }
  });
};

export const checkIsSupportFacialRecognition = (token, checkAliveType, success) => {
  //检查设备是否支持人脸识别
  wx.checkIsSupportFacialRecognition({
    checkAliveType: checkAliveType,
    success() {
      success && success();
    },
    fail(res) {
      uni.showModal({
        title: '提示',
        content: res.errMsg || '当前设备不支持人脸核身操作，请更换设备后重试',
        showCancel: false,
      });
      if (res.errMsg && res.errMsg.indexOf('开发者工具') === -1) {
        reportError(token, 'checkIsSupportFacialRecognition', res, {
          checkAliveType: checkAliveType,
        });
      }
    },
  });
};

// check network when record page，because the camera cover-view error toast display problem，so need back to guide page first, afterwards show the error
export const checkRecordNetworkOk = (callback, error) => {
  uni.getNetworkType({
    success: (res) => {
      if (res.networkType !== 'none') {
        console.log('network is OK')
        callback(res.networkType)
      } else {
        console.log('Network is none')
        error({ ErrorCode: 101, ErrorMsg: '网络异常，请稍后再试' })
      }
    },
    fail: (err) =>  {
      console.log('Get Network exception')
      error({ ErrorCode: 101, ErrorMsg: '网络异常，请稍后再试' })
    }
  })
}